package com.example.schooljava.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.schooljava.pojo.bo.UploadBO;
import com.example.schooljava.pojo.dto.UploadDTO;
import com.example.schooljava.pojo.entity.Teacher;
import com.example.schooljava.mapper.TeacherMapper;
import com.example.schooljava.pojo.bo.StudentsOfClassBO;
import com.example.schooljava.pojo.bo.StudentsScoreOfClassBO;
import com.example.schooljava.pojo.bo.TeachersBO;
import com.example.schooljava.service.TeacherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.schooljava.util.Base64Util;
import com.example.schooljava.util.HttpStatus;
import com.example.schooljava.util.Result;
import com.example.schooljava.util.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author LDH
 * @since 2022/08/11
 */
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService {

    @Autowired
    private TeacherMapper teacherMapper;
    @Autowired
    private TokenUtil tokenUtil;

    /**
     * 功能描述: 用户登录<br>
     * @Param: [account, password]
     * @Return: com.example.schooljava.util.Result
     * @Author: LDH
     * @Date: 2022/8/23 17:18
     */
    @Override
    public Result teacherLogin(String account,String password) {
        List<Teacher> teachers = list();
        for (Teacher t : teachers) {
            if (Objects.equals(t.getAccount(), account) && Objects.equals(t.getPassword(), password)){
                // 获取用户身份
                String[] role = teacherMapper.getRole(t.getTno());
                // 生成token
                String token = tokenUtil.getToken(t.getTno().toString(),Arrays.toString(role));
                return new Result<>().GetToken(t,token);
            }
        }
        return new Result<>().error("","账号或密码错误！", HttpStatus.CONFLICT);
    }


    /**
     * 功能描述: 用户注册<br>
     * @Param: [teacher]
     * @Return: com.example.schooljava.util.Result
     * @Author: LDH
     * @Date: 2022/8/23 17:35
     */
    @Override
    public Result teacherRegister(Teacher teacher) {
        List<Teacher> teachers = list();
        for (Teacher t : teachers) {
            if (Objects.equals(t.getAccount(), teacher.getAccount()))
                return new Result<>().error("","注册失败，账号已存在!",HttpStatus.CONFLICT);
        }
        save(teacher);
        return new Result<>().OK(true,"注册成功!");
    }

    /**
     * 功能描述: 班主任查询学生总成绩<br>
     * @Param: [query]
     * @Return: com.example.schooljava.util.Result
     * @Author: LDH
     * @Date: 2022/8/31 15:33
     */
    @Override
    public Result findAllScore(StudentsOfClassBO studentsOfClassBO) {
        return new Result<>().Huoqu_OK(teacherMapper.findAllScore(studentsOfClassBO));
    }

    /**
     * 功能描述: 获取某个班的所有学生<br>
     * @Param: [studentsOfClassBO]
     * @Return: com.example.schooljava.util.Result
     * @Author: LDH
     * @Date: 2022/9/1 9:56
     */
    @Override
    public Result findOneScore(StudentsOfClassBO studentsOfClassBO) {
        return new Result<>().Huoqu_OK(teacherMapper.findOneScore(studentsOfClassBO));
    }

    /**
     * 功能描述: 获取某个班的所有科任老师<br>
     * @Param: [teacher]
     * @Return: com.example.schooljava.util.Result
     * @Author: LDH
     * @Date: 2022/9/1 9:55
     */
    @Override
    public Result getTeacherList(Long teacher) {
        return new Result<>().Huoqu_OK(teacherMapper.getTeacherList(teacher));
    }

    /**
     * 功能描述: 获取科任老师有教的所有班级的所有学生<br>
     * @Param: [studentsOfClassBO]
     * @Return: com.example.schooljava.util.Result
     * @Author: LDH
     * @Date: 2022/9/1 9:54
     */
    @Override
    public Result getStudentOfCurr(StudentsOfClassBO studentsOfClassBO) {
        return new Result<>().Huoqu_OK(teacherMapper.getStudentOfCurr(studentsOfClassBO));
    }

    /**
     * 功能描述: 获取某位教师有教的年级<br>
     * @Param: [teacherid]
     * @Return: com.example.schooljava.util.Result
     * @Author: LDH
     * @Date: 2022/9/1 9:54
     */
    @Override
    public Result getGrade(Long teacherid) {
        return new Result<>().Huoqu_OK(teacherMapper.getGrade(teacherid));
    }

    /**
     * 功能描述: 获取指定年级下某位教师有任教的班级<br>
     * @Param: [teacherid, grade]
     * @Return: com.example.schooljava.util.Result
     * @Author: LDH
     * @Date: 2022/9/1 9:53
     */
    @Override
    public Result getClassName(Long teacherid, Integer grade) {
        return new Result<>().Huoqu_OK(teacherMapper.getClassName(teacherid,grade));
    }

    /**
     * 功能描述: 获取所有教师<br>
     * @Param: [teachersBO]
     * @Return: com.example.schooljava.util.Result
     * @Author: LDH
     * @Date: 2022/9/1 9:53
     */
    @Override
    public Result getTeacher(TeachersBO teachersBO) {
        return new Result<>().Huoqu_OK(teacherMapper.getTeacher(teachersBO));
    }

    /**
     * 功能描述: 删除教师<br>
     * @Param: [list]
     * @Return: com.example.schooljava.util.Result
     * @Author: LDH
     * @Date: 2022/9/1 9:52
     */
    @Override
    public Result delectTeacher(ArrayList<String> list) {
        for (String l : list) {
            for (Teacher t : list()) {
                if (Long.parseLong(l) == t.getTno())
                    removeById(Long.parseLong(l));
            }
        }
        return new Result<>().OK_Back(true,"删除成功",teacherMapper.getTeacher(new TeachersBO()));
    }

    /**
     * 功能描述: 班级助理获取本班学生成绩<br>
     * @Param: [query]
     * @Return: com.example.schooljava.util.Result
     * @Author: LDH
     * @Date: 2022/8/19 9:37
     */
    @Override
    public Result getStudentScoreOfClass(StudentsScoreOfClassBO studentsScoreOfClassBO) {
        return new Result<>().Huoqu_OK(teacherMapper.getStudentScoreOfClass(studentsScoreOfClassBO));
    }

    /**
     * 功能描述: 获取指定id用的权限，返回权限对应路由表<br>
     * @Param: [tno]
     * @Return: com.example.schooljava.util.Result
     * @Author: LDH
     * @Date: 2022/8/23 17:17
     */
    @Override
    public Result getRouter(Long tno) {
        return new Result<>().Huoqu_OK(teacherMapper.getRoute(tno));
    }

    /**
     * 功能描述: 用户保存头像<br>
     * @Param: [base64]
     * @Return: com.example.schooljava.util.Result<?>
     * @Author: LDH
     * @Date: 2022/8/29 15:36
     */
    @Override
    public Result saveUpload(UploadBO uploadBO) {
        // 获取根目录地址
        String staticPath = System.getProperty("user.dir") + "\\src\\main\\resources\\static\\";
        // 生成头像图片地址
        String path = staticPath+uploadBO.getId()+".jpg";
        // 将base64数据填充进图像中
        Base64Util.GenerateImage(uploadBO.getBase64(),path);
        // 将地址存入数据库中
        Teacher teacher = new Teacher();
        teacher.setTno(uploadBO.getId());
        teacher.setAvatar(String.valueOf(uploadBO.getId()));
        updateById(teacher);
        return new Result<>().OK(true,"头像上传成功!");
    }

    /**
     * 功能描述: 获取用户头像<br>
     * @Param: [tno]
     * @Return: com.example.schooljava.util.Result
     * @Author: LDH
     * @Date: 2022/8/29 17:50
     */
    @Override
    public Result getUpload(String tno) {
        QueryWrapper<Teacher> wrapper = new QueryWrapper<>();
        wrapper.eq("avatar",tno);
        if (list(wrapper).size() != 0)
            return new Result<>().Huoqu_OK(null);
        return new Result<>().error(null,"没有头像",HttpStatus.NO_CONTENT);
    }
}
